package com.mock.action.contact;

import java.util.ArrayList;
import java.util.List;

import com.mock.action.AbstractAction;
import com.mock.dao.BestContactMethodDao;
import com.mock.dao.ContactDao;
import com.mock.dao.ContactTypeDao;
import com.mock.dao.impl.BestContactMethodDaoImpl;
import com.mock.dao.impl.ContactDaoImpl;
import com.mock.dao.impl.ContactTypeDaoImpl;
import com.mock.model.BestContactMethod;
import com.mock.model.Contact;
import com.mock.model.ContactType;
import com.mock.model.DataTable;

public class ContactAction extends AbstractAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<Contact> contacts;
	private Contact contact;
	private List<ContactType> contactTypes;
	private List<BestContactMethod> bestContactMethods;
	private ContactTypeDao contactTypeDao = new ContactTypeDaoImpl();
	private BestContactMethodDao bestContactMethodDao = new BestContactMethodDaoImpl();

	
	private List<ArrayList<String>> aaData = new ArrayList<ArrayList<String>>();
	private String sEcho;
	private String sSearch;
	private int iDisplayLength;
	private int iDisplayStart;
	private int iTotalRecords;
	private int iTotalDisplayRecords;
	private int iSortCol_0;
	private String sSortDir_0;
	private String sSearch_5;
	private String sSearch_6;
	private String sSearch_7;
	
	@Override
	public String execute() throws Exception {	
		ContactDao contactDao = new ContactDaoImpl();
		contacts = contactDao.getAllContact();
		generateData();
		return SUCCESS;
	}	
	
	public void generateData() {
		contactTypes = new ArrayList<ContactType>();
		contactTypes = contactTypeDao.getAllContactType();
		bestContactMethods = new ArrayList<BestContactMethod>();
		bestContactMethods = bestContactMethodDao.getAllBestContactMethod();
	}		
	

	public List<Contact> getContacts() {
		return contacts;
	}
	
	//contact table ajax
	public String contactTableAjax() {
		DataTable dt = new DataTable();
		dt.sEcho = sEcho = getRequest().getParameter("sEcho");		
		dt.iDisplayStart = iDisplayStart = Integer.parseInt(getRequest()
				.getParameter("iDisplayStart"));
		dt.iDisplayLength = iDisplayLength = Integer.parseInt(getRequest()
				.getParameter("iDisplayLength"));
		dt.sSearch_5 = sSearch_5 = getRequest().getParameter("sSearch_5");
		dt.sSearch_6 = sSearch_6 = getRequest().getParameter("sSearch_6");
		dt.sSearch_7 = sSearch_7 = getRequest().getParameter("sSearch_7");
		dt.iSortCol_0 = iSortCol_0 = Integer.parseInt(getRequest()
				.getParameter("iSortCol_0"));
		dt.sSortDir_0 = sSortDir_0 = getRequest().getParameter("sSortDir_0");
		dt.iTotalDisplayRecords = iTotalDisplayRecords = new ContactDaoImpl()
				.getRowCount();
		dt.iTotalRecords = iTotalRecords = new ContactDaoImpl().getRowCount();
		List<Contact> contacts = new ContactDaoImpl().getContactByQuery(dt);
		for (Contact contact : contacts) {
			ArrayList<String> newArray = new ArrayList<String>();
			newArray.add(Long.toString(contact.getId()));
			newArray.add(contact.getFirstname() + " " + contact.getSurname());
			newArray.add(contact.getMobilePhone());
			newArray.add(contact.getEmail());			
			newArray.add(contact.getContactType().getType());			
			if (contact.isActive()) {
				newArray.add("Yes");
			} else {
				newArray.add("No");
			}			
			newArray.add(contact.getFirstname());
			newArray.add(contact.getSurname());
			aaData.add(newArray);
		}
		return SUCCESS;
	}
	
	public void setContacts(List<Contact> contacts) {
		this.contacts = contacts;
	}

	public Contact getContact() {
		return contact;
	}

	public void setContact(Contact contact) {
		this.contact = contact;
	}

	public List<ContactType> getContactTypes() {
		return contactTypes;
	}

	public void setContactTypes(List<ContactType> contactTypes) {
		this.contactTypes = contactTypes;
	}

	public List<BestContactMethod> getBestContactMethods() {
		return bestContactMethods;
	}

	public void setBestContactMethods(List<BestContactMethod> bestContactMethods) {
		this.bestContactMethods = bestContactMethods;
	}	

	public List<ArrayList<String>> getAaData() {
		return aaData;
	}

	public void setAaData(List<ArrayList<String>> aaData) {
		this.aaData = aaData;
	}

	public String getsEcho() {
		return sEcho;
	}

	public void setsEcho(String sEcho) {
		this.sEcho = sEcho;
	}

	public String getsSearch() {
		return sSearch;
	}

	public void setsSearch(String sSearch) {
		this.sSearch = sSearch;
	}

	public int getiDisplayLength() {
		return iDisplayLength;
	}

	public void setiDisplayLength(int iDisplayLength) {
		this.iDisplayLength = iDisplayLength;
	}

	public int getiDisplayStart() {
		return iDisplayStart;
	}

	public void setiDisplayStart(int iDisplayStart) {
		this.iDisplayStart = iDisplayStart;
	}

	public int getiTotalRecords() {
		return iTotalRecords;
	}

	public void setiTotalRecords(int iTotalRecords) {
		this.iTotalRecords = iTotalRecords;
	}

	public int getiTotalDisplayRecords() {
		return iTotalDisplayRecords;
	}

	public void setiTotalDisplayRecords(int iTotalDisplayRecords) {
		this.iTotalDisplayRecords = iTotalDisplayRecords;
	}

	public int getiSortCol_0() {
		return iSortCol_0;
	}

	public void setiSortCol_0(int iSortCol_0) {
		this.iSortCol_0 = iSortCol_0;
	}

	public String getsSortDir_0() {
		return sSortDir_0;
	}

	public void setsSortDir_0(String sSortDir_0) {
		this.sSortDir_0 = sSortDir_0;
	}

	public String getsSearch_5() {
		return sSearch_5;
	}

	public void setsSearch_5(String sSearch_5) {
		this.sSearch_5 = sSearch_5;
	}

	public String getsSearch_6() {
		return sSearch_6;
	}

	public void setsSearch_6(String sSearch_6) {
		this.sSearch_6 = sSearch_6;
	}

	public String getsSearch_7() {
		return sSearch_7;
	}

	public void setsSearch_7(String sSearch_7) {
		this.sSearch_7 = sSearch_7;
	}
	

}
